package com.twu.biblioteca;

import com.twu.exception.MenuException;

import java.util.ArrayList;

public class Menu {
    ArrayList<String> menuItems = new ArrayList<String>();

    public boolean hasItem(String item) {
        return menuItems.contains(item);
    }

    public void add(String item) throws MenuException{
        if(menuItems.contains(item)){
            throw new MenuException("Item already in the Menu!");
        }
        menuItems.add(item);
    }

    public void remove(String item) throws MenuException {
        if(!menuItems.contains(item)){
            throw new MenuException("Item not in the Menu!");
        }
        menuItems.remove(item);
    }

    public String items() {
        String menu = LibraryConstants.MENU_FIRST_LINE;
        int count = 1;
        for (String item : menuItems) {
            menu += count + " - " + item + "\n";
            count++;
        }
        return menu;
    }

    public int select(String item) throws MenuException{
        try{
            return menuItems.indexOf(menuItems.get(Integer.parseInt(item) - 1)) + 1;
        }catch(Exception iex){
            throw new MenuException(LibraryConstants.INVALID_MENU_SELECTION);
        }
    }
}

